package com.puccampinas.finalproject.rest;

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.eaio.uuid.UUID;
import com.puccampinas.finalproject.core.controller.UserController;
import com.puccampinas.finalproject.core.exceptions.NullUserException;
import com.puccampinas.finalproject.core.persistence.entity.Client;
import com.puccampinas.finalproject.rest.to.RequestResponse;
import com.puccampinas.finalproject.rest.to.ResponseStatus;

@Path("/api/user")
public class UserHandler {

	@GET
    @Path("register/{userPhone}/{userPass}")
    @Produces({MediaType.APPLICATION_XML})
	public RequestResponse register(@PathParam("userPhone") String celphone, @PathParam("userPass") String password) {

		RequestResponse response = new RequestResponse();
		response.setDate(new Date());
		response.setStatus(ResponseStatus.OK);
		response.setTicket(new UUID().toString());
		response.setMessage("SUCESSOOOOOOOOO");
		
        Client cliente = new Client();
        cliente.setCelphone(celphone);
        cliente.setPassword(password);
        cliente.setName(celphone);
        cliente.setUsername(celphone);
	
		try {
			UserController.register(cliente);
		} catch (NullUserException e) {
			e.printStackTrace();
			response.setStatus(ResponseStatus.ERROR);
			response.setMessage("ERROOOOOOOO");
		}
	    
        return response;
		
	}
	
	@GET
    @Path("enable/{userPhone}")
    @Produces({MediaType.APPLICATION_XML})
	public RequestResponse enable(@PathParam("userPhone") String celphone) {

		RequestResponse response = new RequestResponse();
		response.setDate(new Date());
		response.setStatus(ResponseStatus.OK);
		response.setTicket(new UUID().toString());
		response.setMessage("SUCESSOOOOOOOOO");
		
        Client cliente = new Client();
        cliente.setUsername(celphone);
	
		try {
			UserController.enable(cliente);
		} catch (NullUserException e) {
			response.setStatus(ResponseStatus.ERROR);
			response.setMessage("ERROOOOOOOO");
		}
	    
        return response;
		
	}
}
